package internet_02;
//UDP发送数据

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Demo01 {
    public static void main(String[] args) throws IOException {
       /*DatagramSocket ds=new DatagramSocket();
       //打包数据
        byte[] bys="往后余生".getBytes();
        InetAddress adress = InetAddress.getByName("127.0.0.1");
        DatagramPacket dp =new DatagramPacket(bys,bys.length,adress,10000);

        ds.send(dp);
        ds.close();*/

        //接收数据
        DatagramSocket ds = new DatagramSocket(10000);
        byte[] bys=new byte[1024];
        DatagramPacket dp=new DatagramPacket(bys,bys.length);
        ds.receive(dp);
        //解析数据
        byte[] data = dp.getData();
        int length = dp.getLength();
        String s=new String(data,0,length);
        System.out.println(s);

        ds.close();
    }
}
